import { trigger } from '@/utiils/util';

export default {}.install = (Vue, options = {}) => {
  Vue.directive('int', {
    bind(el) {
      const nodeName = el.nodeName.toLocaleLowerCase();
      const input = nodeName === 'input' ? el : el.getElementsByTagName('input')[0];
      if (!input) {
        return;
      }
      input.onkeyup = (e) => {
        input.value = input.value.replace(/[^\d]/g, '');
        trigger(input, 'input');
      };
      input.onblur = (e) => {
        input.value = parseFloat(input.value.replace(/[^\d]/g, '')) || 0;
      };
      trigger(input, 'input');
    }
  });
};
